﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyDaiLy.View
{
    /// <summary>
    /// Interaction logic for Main.xaml
    /// </summary>
    public partial class Main : Window
    {
        #region PRIVATE VARIABLE
        private bool isMove = false;
        private string _title = "Phần mềm quản lý phòng mạch tư - ";
        private wLogin _login;
        private List<int> _history;
        #endregion

        #region PRIVATE FUNCTION
        //Hàm lấy tiêu đề cho form
        private string getTitle(int id)
        {
            switch (id)
            {
                case 0: return "Quản lý";
                case 1: return "Nghiệp vụ";
                case 2: return "Hệ thống";
                case 3: return "Tài khoản";
                default: return "Phần mềm";
            }
        }
        //Hàm xử lí cho phần Nghiệp vụ
        private void nghiepvuVisibility(int id)
        {
            switch (id)
            { 
                case 1:
                    NVDaiLy.Visibility = Visibility.Visible;
                    NVMatHang.Visibility = NVPhieuThu.Visibility = NVPhieuXuat.Visibility = Visibility.Collapsed;
                    break;
                case 2:
                    NVMatHang.Visibility = Visibility.Visible;
                    NVDaiLy.Visibility = NVPhieuThu.Visibility = NVPhieuXuat.Visibility = Visibility.Collapsed;
                    break;
                case 3:
                    NVPhieuXuat.Visibility = Visibility.Visible;
                    NVMatHang.Visibility = NVDaiLy.Visibility = NVPhieuThu.Visibility = Visibility.Collapsed;
                    break;
                default:
                    NVPhieuThu.Visibility = Visibility.Visible;
                    NVPhieuXuat.Visibility = NVMatHang.Visibility = NVDaiLy.Visibility = Visibility.Collapsed;
                    break;

            }
        }
        //Hàm xử lí cho phần hệ thống
        private void hethongVisibility(int id)
        {
            switch (id)
            { 
                case 1:
                    HTDonVi.Visibility = Visibility.Visible;
                    HTLoaiDaiLy.Visibility = HTQuan.Visibility = Visibility.Collapsed;
                    break;
                case 2:
                    HTLoaiDaiLy.Visibility = Visibility.Visible;
                    HTDonVi.Visibility = HTQuan.Visibility = Visibility.Collapsed;
                    break;
                default:
                    HTQuan.Visibility = Visibility.Visible;
                    HTLoaiDaiLy.Visibility = HTDonVi.Visibility = Visibility.Collapsed;
                    break;
            }
        }
        //Hàm xử lí hiển thị cho phần Tài khoản
        private void taikhoanVisibility(int id)
        {
            switch (id)
            {
                case 1:
                    TKQuanLy.Visibility = Visibility.Collapsed;
                    TKThongTin.Visibility = Visibility.Visible;
                    break;
                default:
                    TKQuanLy.Visibility = Visibility.Visible;
                    TKThongTin.Visibility = Visibility.Collapsed;
                    break;
            }
        }

        private void addPage()
        {
            Page page;
            Frame frm;
            //Đại lý
            frm = new Frame();
            page = new WDaiLy();
            frm.Navigate(page);
            NVDaiLy.Children.Add(frm);
            //Mặt hàng
            frm = new Frame();
            page = new WMatHang();
            frm.Navigate(page);
            NVMatHang.Children.Add(frm);

            //Phiếu xuất
            frm = new Frame();
            page = new WPhieuXuatHang();
            frm.Navigate(page);
            NVPhieuXuat.Children.Add(frm);

            
        }
        #endregion

        #region CONSTRUCTOR
        public Main(wLogin parent)
        {
            InitializeComponent();
            this._login = parent;
            _history = new List<int>();

        }

        public void TaoLienKet()
        {
            addPage();
        }
        #endregion

        private void smTTTaiKhoan_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            taikhoanVisibility(1);
        }

        private void smQLTaiKhoan_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            taikhoanVisibility(2);
        }
        private void btnExit_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
            this._login.Show();
        }
        private void smDangXuat_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            this._login.Show();
            this.Close();
        }
        private void Grid_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            isMove = true;
        }

        private void Grid_MouseMove_1(object sender, MouseEventArgs e)
        {
            try
            {
                if (isMove) this.DragMove();
            }
            catch { }
        }

        private void Grid_MouseUp_1(object sender, MouseButtonEventArgs e)
        {
            isMove = false;
        }
        private void TabMenu_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            string tmp = getTitle(TabMenu.SelectedIndex);
            this.Title = _title + tmp;
            if (lbLichSu.Items.IndexOf(tmp) < 0)
                lbLichSu.Items.Add(tmp);
            _history.Add(TabMenu.SelectedIndex);
            _history = _history.Distinct().ToList();
        }
        private void lbLichSu_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            TabMenu.SelectedIndex = _history[lbLichSu.SelectedIndex];
        }

        private void smDaiLy_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(1);
        }

        private void smMatHang_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(2);
        }

        private void smPhieuXuat_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(3);
        }

        private void smPhieuThu_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(4);
        }
    }
}
